<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Hook outcome - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/hooks/poke_exception.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/hooks.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">Hook outcome</h1></div></div></div>
  <p>The final step is to add event hooks for the very specific case of
when our localised <code>outcome</code> is copy or move constructed from our localised <code>result</code>.</p>

<p>You ought to be very careful that the <code>noexcept</code>-ness of these matches the <code>noexcept</code>-ness
of the types in the <code>outcome</code>. You may have noticed that <code>poke_exception()</code> creates
a <code>std::string</code> and appends to it. This can throw an exception. If the copy and/or
move constructors of <code>T</code>, <code>EC</code> and <code>EP</code> are <code>noexcept</code>, then so will be <code>outcome</code>&rsquo;s
copy and/or move constructor. Thus if <code>poke_exception()</code> throws, instant program
termination would occur, which is bad.</p>

<p>We avoid that problem in this case by wrapping <code>poke_exception()</code> in a <code>try...catch</code>
which throws away any exceptions thrown. For Outcome before v2.2, these specially
named free functions must be placed into a namespace which is ADL searched:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">namespace</span> <span class="n">error_code_extended</span>
<span class="p">{</span>
  <span class="c1">// Specialise the outcome copy and move conversion hook for when our localised result
</span><span class="c1"></span>  <span class="c1">// is used as the source for copy construction our localised outcome
</span><span class="c1"></span>  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">T</span><span class="p">,</span> <span class="k">class</span><span class="err"> </span><span class="nc">U</span><span class="o">&gt;</span> <span class="kr">inline</span> <span class="kt">void</span> <span class="n">hook_outcome_copy_construction</span><span class="p">(</span><span class="n">outcome</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="o">*</span><span class="n">res</span><span class="p">,</span> <span class="k">const</span> <span class="n">result</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span> <span class="o">&amp;</span> <span class="cm">/*unused*/</span><span class="p">)</span> <span class="k">noexcept</span>
  <span class="p">{</span>
    <span class="k">try</span>
    <span class="p">{</span>
      <span class="c1">// when copy constructing from a result&lt;T&gt;, poke in an exception
</span><span class="c1"></span>      <span class="n">poke_exception</span><span class="p">(</span><span class="n">res</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">catch</span><span class="p">(...)</span>
    <span class="p">{</span>
      <span class="c1">// Do nothing
</span><span class="c1"></span>    <span class="p">}</span>
  <span class="p">}</span>
  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">T</span><span class="p">,</span> <span class="k">class</span><span class="err"> </span><span class="nc">U</span><span class="o">&gt;</span> <span class="kr">inline</span> <span class="kt">void</span> <span class="n">hook_outcome_move_construction</span><span class="p">(</span><span class="n">outcome</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="o">*</span><span class="n">res</span><span class="p">,</span> <span class="n">result</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span> <span class="o">&amp;&amp;</span> <span class="cm">/*unused*/</span><span class="p">)</span> <span class="k">noexcept</span>
  <span class="p">{</span>
    <span class="k">try</span>
    <span class="p">{</span>
      <span class="c1">// when move constructing from a result&lt;T&gt;, poke in an exception
</span><span class="c1"></span>      <span class="n">poke_exception</span><span class="p">(</span><span class="n">res</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">catch</span><span class="p">(...)</span>
    <span class="p">{</span>
      <span class="c1">// Do nothing
</span><span class="c1"></span>    <span class="p">}</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/error_code_extended.cpp#L182" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>For Outcome v2.2 and later, these functions must be placed into a custom no value
policy with the names <code>on_outcome_copy_construction()</code> and <code>on_outcome_move_construction()</code>
respectively. As with before, the implementation of the functions is identical, just
the name and location has changed.</p>


        </div><p><small>Last revised: December 15, 2020 at 12:22:39 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/hooks/poke_exception.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/hooks.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>
